<?php
  //This has to be the first line of code a file if sessions are used.
  session_start();
  
  require_once ('check_PDO.php');
  require_once ('appinfo.php');
  require_once ('configuration.php');

  //default values
  $host = "localhost";
  $user = "public";
  $pass = "";  
  $database = "demodb";  

  $c = new Configuration();
  $ver = $c->VERSION;
  $host = $c->DB_HOST;
  $user = $c->DB_USER;
  $pass = $c->DB_PASSWORD;  
  $database = $c->DB_NAME;  

  $error = "";

  if ($_POST) {
    if (isset($_POST['HOST']))
      $host = $_POST['HOST'];

    if (isset($_POST['USER']))
      $user = $_POST['USER'];

    if (isset($_POST['PASS']))
      $pass = $_POST['PASS'];

    if (isset($_POST['DATABASE']))
      $database = $_POST['DATABASE'];
  }

  $connection_valid = false;
  if ($host && $user && ($pass || $_POST)) {
    try {
      //cubrid:host=localhost;port=33000;dbname=demodb
      $conn_str = "cubrid:dbname=".$database.";host=".$host.";port=30000";
      $db = new PDO($conn_str, $user, $pass );
      $connection_valid = true;
      $db = null; //disconnect
    } catch(PDOException $e) {
      //echo "Error: ".$e->getMessage()."<br />";
    }
 
    if($connection_valid) {
      AppInfo::Load()->ConnURL = "cubrid://".$user.":".$pass."@".$host."/".$database;
      $_SESSION['AppInfo'] = serialize(AppInfo::Load());

      $path = $_SERVER["SCRIPT_NAME"];
      $pathSplit = explode("/", $path);

      $redirect = "";

      for ($i=0; $i<count($pathSplit)-1; $i++) {
        if (trim($pathSplit[$i]) != "") {
          $redirect .= "/" . $pathSplit[$i];
        }
      }

      if (array_key_exists("HTTPS", $_SERVER) && $_SERVER['HTTPS'] == "on") {
        $protocol = "https://";
      } else {
        $protocol = "http://";
      }

      $redirect = $protocol . $_SERVER["HTTP_HOST"] . $redirect . "/index.php";
      $error = "";
      header("Location: ".$redirect);
      exit;
    } else {
      $error = "There was a problem logging you in.\\nPlease verify:\\n- the credentials you entered: [".$user.":".$password."]\\n- the CUBRID Service is up and running on [".$host."]\\n- the [".$database."] database is started";
    }
  }                      
?>

<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <?php include("head.php"); ?>
  </head>
  <body bgcolor="#FAFAFA">
    <br />
    <br />
    <table border="2px" align="center" style="padding: 20px; margin: 20px; border-collapse: collapse;" width="600px;" bgcolor="#FFFFFF">
    <tr>
    <td>
      <br />
      <br />
      <br />
      <table cellpadding="0" align="center">
        <tr>
          <td>
            <p class="title">CUBRID Database Schema <?php echo $ver; ?></p>
          </td>
        </tr>
      </table>
      <br />
      <form name="loginform" method="post" action="">
        <table cellpadding="0" align="center">
          <tr>
            <td><div class="label"><?php echo "Host[:Port]:"; ?></div></td>
            <td><input type="text" class="input" title="Enter &#60;host name&#62;:&#60;port&#62;" name="HOST" value="<?php echo $host; ?>" /></td>
          </tr>
          <tr>
            <td><div class="label"><?php echo "Username:"; ?></div></td>
            <td><input type="text" class="input" title="User name" name="USER" value="<?php echo $user; ?>"/></td>
          </tr>
          <tr>
            <td><div class="label"><?php echo "Password:"; ?></div></td>
            <td><input type="password" class="input" title="Password" name="PASS" id="PASS" value="<?php echo $pass; ?>" /></td>
          </tr>
          <tr>
            <td><div class="label"><?php echo "Database:"; ?></div></td>
            <td><input type="text" class="input" title="CUBRID Database (mandatory)" name="DATABASE" id="DATABASE" value="<?php echo $database; ?>" /></td>
          </tr>
        </table>
        <br />
        <table cellpadding="0" align="center">
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" class="button" title="Login to CUBRID" value="Login" accesskey="l" /></td>
          </tr>
        </table>
        <br />
        <br />
        <br />
        <table cellpadding="0" align="center">
          <tr align="center">
            <td align="center"><a href="http://code.google.com/p/cubrid-database-schema/" target="_blank" title="Visit project home page">Google Code - Project home page</a></td>
          </tr>
          <tr align="center">
            <td align="center"><br /></td>
          </tr>
          <tr align="center">
            <td align="center" style="margin: 0px" width="100%">
              <table width="100%">
                <tr>
                  <td align="left" width="40%">
                    <a href="http://www.cubrid.org" target="_blank" title="Visit CUBRID web site">
                      <img src="images/cubrid-logo.png" alt="Visit CUBRID web site - www.cubrid.org" border=0 style="margin: 0px; padding: 0px;" />
                    </a>
                  </td>
                  <td align="center" width="100px">
                  </td>
                  <td align="right" width="40%">
                    <a href="http://www.cubrid.org/projects" target="_blank" title="Visit CUBRID Projects">
                      <img src="images/cubrid-projects-logo.png" alt="Visit CUBRID projects - www.cubrid.org/projects" border=0 style="margin: 0px; padding: 0px;" />
                    </a>
                  </td>
                </tr>
              </table>
              <br />
            </td>
          </tr>
        </table>
      </form>
    </td>
    </tr>
    </table>

    <?php include("cookies_check.php"); ?> 
    
    <?php 
    if(strlen($error) > 0) {
      echo "<script type=\"text/javascript\">".PHP_EOL;
      echo "<!--".PHP_EOL;
      echo "alert('Errror: ".$error."');".PHP_EOL;
      echo "// -->".PHP_EOL;
      echo "</script>".PHP_EOL;
    }
    ?>     
  </body>
</html>